<?php
/**
 * 用户处理model
 * Created by Forever.
 * User: Forever
 * Date: 2018/10/03 
 * Time: 上午 10:39
 */
namespace app\api\model;
use think\Cache;
use \think\Model;
use \think\db;
use EasyWeChat\Foundation\Application;
class Member extends Base{

    public function initialize() { } 

    /*
    * 获取会员详情
    * @param data
    * @return object
    */
    public function getMemberDetail($data=[]){
        $where = [
            "openid"=>$data['openid']
        ];
        $field = "id,name,nickname,cs_id,mobile,headimg,province,city,district,address,addtime";
        $res =  Db::name(self::$tb['member'])->where($where)->field($field)->find();
        if($res){
            return $res;
        }else{
            return false;
        }
    }


   /*
    * 获取会员收藏的店铺列表
    * @param data
    * @return object
    */
    public function getShopFollow($data=[]){ 
        $where = [
            "f.member_id"=>$data['member_id'],
            "s.ischeck"=>1
        ]; 
        $data['page'] = isset($data['page'])?$data['page']:1;
        $data['rows'] = isset($data['rows'])?$data['rows']:10;
        $field = "s.id,s.name,s.province,s.city,s.district,s.detail,s.shop_pic,f.addtime";
        $result['total'] =  Db::name(self::$tb['follow_shop'])->alias("f")->join("jun_shop s",'s.id=f.shop_id')
        ->where($where)->count(); 
        $list =  Db::name(self::$tb['follow_shop'])->alias("f")->join("jun_shop s",'s.id=f.shop_id')
        ->where($where)->field($field)->page($data['page'],$data['rows'])->select(); 
        if(!empty($list)){
            foreach ($list as $key => $value) {
                $prepAddr = $value['province'].$value['city'].$value['district'].$value['detail'];
                $list[$key]['addr'] = $prepAddr;
                $list[$key]['latng'] = $this->get_localtion($data['latitude'],$data['longitude'],$prepAddr);
            }
        }
        $result['list'] = $list;
        return $result??[];
    }

   /*
    * 获取定位
    */
    public function get_localtion($latitude,$longitude,$prepAddr){  
       $data = $this->data;
       $key = config("baiduKey"); 
       $latng = getLatng($prepAddr); 
       $url = "http://api.map.baidu.com/routematrix/v2/driving?output=json&origins=".$latitude.",".$longitude."&destinations=".$latng['lat'].",".$latng['lng']."&ak=".$key;  
       $geocode = file_get_contents($url);
       $output= json_decode($geocode,true);  
       if(!empty($output['result'])){
          $latng = $output['result'][0]['distance']['text']; 
          return $latng;
        }else{
          return $output;
        }
    }
    

  /*
    * 获取会员收藏的汽车列表
    * @param data
    * @return object
    */
    public function getCarFollow($data=[]){
        $where = [];
        $data['page'] = isset($data['page'])?$data['page']:1;
        $data['rows'] = isset($data['rows'])?$data['rows']:10;
        $field = "id,product_id,title,pic,first_pay,monthly_pay,attr,addtime";
        $list['list'] =  Db::name(self::$tb['follow_product'])->where($where)->field($field)->order("addtime desc")->page($data['page'],$data['rows'])->select();  
        $list['total'] = Db::name(self::$tb['follow_product'])->where($where)->count();
        return $list; 
    }

    



 }   
